package com.concerneyes;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.Ssiwo.sdk.SsiwoFacenginActivity;
import com.concerneyes.glasses.GlassesView;
import com.concerneyes.health.HealthActivity;
import com.concerneyes.mine.MineView;
import com.concerneyes.mine.TabActivity.MyExtension;
import com.concerneyes.mine.TabActivity.serviceCentre.OnLineService;
import com.concerneyes.rentbarsecond.RentBarNewView;
import com.concerneyes.rentbarsecond.adapter.NewRentBarAdapter;
import com.concerneyes.task.DeviceInfo;
import com.concerneyes.titleView.MsgActivity;
import com.concerneyes.trytoglasses.TryToGlassesView;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.Tencent;
import com.unity3d.player.UnityPlayer;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;
import common.AbsActivity;
import common.AbsActivityForSsiwo;
import common.AbsBaseView;
import common.PreferUtil;
import http.LoginTask;
import util.ActivityUtil;
import util.Util;
import view.MyUniversalDialog;
import view.PopMenu;

public class MainActivity extends AbsActivityForSsiwo implements View.OnClickListener, PopMenu.OnMenuItemClickListener {
    private HashMap<Integer, AbsBaseView> viewStack;
    private FrameLayout contentLayout;
    private LinearLayout ll_bottombar;
    protected UnityPlayer mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String registrationID = JPushInterface.getRegistrationID(this);
        PreferUtil.cacheRegistrationID(registrationID);
        this.mPlayer = mUnityPlayer;
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        if (getTabIndex() == 0) {
            TryToGlassesView tryToGlassesView = (TryToGlassesView) viewStack.get(0);
            tryToGlassesView.onResume();
        }
        super.onResume();
        if (getTabIndex() == 2) {
            setCurTab(0);
        }
        if (viewStack != null) {
            AbsBaseView childView = viewStack.get(getTabIndex());
            childView.loadData("1");
            mUnityPlayer.resume();
        }

    }

    @Override
    protected void onPause() {
        if (getTabIndex() == 0) {
            TryToGlassesView tryToGlassesView = (TryToGlassesView) viewStack.get(0);
            tryToGlassesView.onPause();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private void initView() {
        getTitleBar().setVisibility(View.VISIBLE);
        setContentView(R.layout.activity_main);
        initBottomBar();
        ll_bottombar = (LinearLayout) findViewById(R.id.ll_bottombar);
        contentLayout = (FrameLayout) findViewById(R.id.content_view);
        ll_bottombar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, -1);
                params.setMargins(0, 0, 0, ll_bottombar.getHeight());
                contentLayout.setLayoutParams(params);
            }
        });
        viewStack = new HashMap<>();
        TryToGlassesView tryToGlassesView = new TryToGlassesView(this, this, mPlayer);
        viewStack.put(0, tryToGlassesView);
        GlassesView glassesView = new GlassesView(this, this);
        viewStack.put(1, glassesView);
        viewStack.put(2, null);
//        RentBarView rentBarView = new RentBarView(this);
        RentBarNewView rentBarView = new RentBarNewView(this);
        viewStack.put(3, rentBarView);
        MineView mineView = new MineView(this);
        viewStack.put(4, mineView);
        setCurTab(0);
    }


    @Override
    public void onRightTextAction(View v) {
        super.onRightTextAction(v);
        if (getTabIndex() == 4) {
            ActivityUtil.setIntent(this, MsgActivity.class, null);
        } else if (getTabIndex() == 1) {
            final MyUniversalDialog dialog = new MyUniversalDialog(this);
            int width = getWindowManager().getDefaultDisplay().getWidth();
            dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_rent_info, null), MyUniversalDialog.DialogGravity.CENTER,
                    -2, 5 * width / 6);
            ImageView ivCancel = (ImageView) dialog.findViewById(R.id.iv_cancel);
            ivCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    }

    @Override
    public void onRightAction(View v) {
        super.onRightAction(v);
        if (getTabIndex() == 0) {
            ActivityUtil.setIntent(this, MyExtension.class, null);
        } else if (getTabIndex() == 3) {
            ActivityUtil.setIntent(this, OnLineService.class, null);
        }
    }

    @Override
    public void onLeftTextAction(View v) {
        super.onLeftTextAction(v);
        if (getTabIndex() == 0 || getTabIndex() == 1) {
            int arrayId = R.array.choose_array;
            int menuId = POP_MENU_CHOOSE_ID;

            String[] menuArray = getResources().getStringArray(arrayId);
            int xoffset = getTitleBar().getWidth() - getResources().getDimensionPixelSize(R.dimen.pop_menu_width) - 2;
            PopMenu menu = new PopMenu(this);
            menu.setOnMenuItemClickListener(this);
            menu.setId(menuId);
            menu.addItems(menuArray, null);
            menu.showAsDropDown(getTitleBar(), 0, 2);
        }
        if (getTabIndex() == 3) {
            RentBarNewView childView = (RentBarNewView) viewStack.get(getTabIndex());
            if (viewStack != null) {
                if (isClick) {
                    childView.deteleItem(isClick);
                    isClick = false;
                } else {
                    childView.deteleItem(isClick);
                    isClick = true;
                }
            }
        }
    }

    private boolean isClick = true;
    //===================bottomBar==============
    private int curTab = -1;
    private RelativeLayout[] tabs = new RelativeLayout[5];
    private ImageView[] tbIvs = new ImageView[tabs.length];
    private TextView[] tbTvs = new TextView[tabs.length];

    private TextView countTv;

    private final int[] tabIconSelected = new int[]{
            R.drawable.bottomtab_shidai_selected,
            R.drawable.bottomtab_chanpin_selected,
            R.drawable.bottomtab_jiankang_unselected,
            R.drawable.bottomtab_zubar_selected,
            R.drawable.bottomtab_wode_selected
    };

    private final int[] tabIconUnselected = new int[]{
            R.drawable.bottomtab_shidai_unselected,
            R.drawable.bottomtab_chanpin_unselected,
            R.drawable.bottomtab_jiankang_unselected,
            R.drawable.bottomtab_zubar_unselected,
            R.drawable.bottomtab_wode_unselected
    };

    private void initBottomBar() {
        countTv = (TextView) findViewById(R.id.countTv);
        tabs[0] = (RelativeLayout) findViewById(R.id.tab_biz);
        tabs[1] = (RelativeLayout) findViewById(R.id.tab_msg);
        tabs[2] = (RelativeLayout) findViewById(R.id.tab_affair);
        tabs[3] = (RelativeLayout) findViewById(R.id.tab_contact);
        tabs[4] = (RelativeLayout) findViewById(R.id.tab_me);

        tbIvs[0] = (ImageView) findViewById(R.id.tab_iv_biz);
        tbIvs[1] = (ImageView) findViewById(R.id.tab_iv_msg);
        tbIvs[2] = (ImageView) findViewById(R.id.tab_iv_affair);
        tbIvs[3] = (ImageView) findViewById(R.id.tab_iv_contact);
        tbIvs[4] = (ImageView) findViewById(R.id.tab_iv_me);

        tbTvs[0] = (TextView) findViewById(R.id.tab_tv_biz);
        tbTvs[1] = (TextView) findViewById(R.id.tab_tv_msg);
        tbTvs[2] = (TextView) findViewById(R.id.tab_tv_affair);
        tbTvs[3] = (TextView) findViewById(R.id.tab_tv_contact);
        tbTvs[4] = (TextView) findViewById(R.id.tab_tv_me);
        for (int i = 0; i < tabs.length; i++) {
            RelativeLayout item = tabs[i];
            item.setTag(String.valueOf(i));
            item.setOnClickListener(MainActivity.this);
        }
    }

    @Override
    public void onClick(View view) {
        int actionIndex = Integer.parseInt(view.getTag().toString());
        if (actionIndex == curTab) {
            return;
        }
        curTab = actionIndex;
        for (int i = 0; i < tabs.length; i++) {
            RelativeLayout item = tabs[i];
            if (item.getId() == view.getId()) {
                tbIvs[i].setImageResource(tabIconSelected[i]);
                tbTvs[i].setTextColor(getResources().getColor(R.color.blue_bottom));
            } else {
                tbIvs[i].setImageResource(tabIconUnselected[i]);
                tbTvs[i].setTextColor(getResources().getColor(R.color.tabTextColor));
            }
        }
        onTabBarChange(curTab, view.getId());
    }

    private void onTabBarChange(int position, int tabId) {
        if (position == 0) {
            getRightBtn().setVisibility(View.VISIBLE);
            getRightText().setVisibility(View.GONE);
            getRightText2().setVisibility(View.GONE);
            getLeftText().setVisibility(View.VISIBLE);
            getRightBtn().setImageResource(R.drawable.nav_ico_hdzx);
            getLeftText().setText(R.string.choose);
            Util.setTriangle(this, getLeftText());
        } else if (position == 1) {
            getRightBtn().setVisibility(View.GONE);
            getRightText().setVisibility(View.VISIBLE);
            getRightText2().setVisibility(View.GONE);
            getLeftText().setVisibility(View.VISIBLE);
            getRightText().setText(R.string.rent_explain);
            getLeftText().setText(R.string.choose);
            Util.setTriangle(this, getLeftText());
        } else if (position == 3) {
            getRightBtn().setVisibility(View.VISIBLE);
            getRightText().setVisibility(View.GONE);
            getRightText2().setVisibility(View.GONE);
            getLeftText().setVisibility(View.VISIBLE);
            getLeftText().setText(R.string.edit);
            getLeftText().setCompoundDrawables(null, null, null, null);
            getRightBtn().setImageResource(R.drawable.nav_ico_kf);

            RentBarNewView childView = (RentBarNewView) viewStack.get(getTabIndex());
            childView.deteleItem(false);
        } else if (position == 4) {
            getRightText().setVisibility(View.VISIBLE);
            getLeftText().setVisibility(View.GONE);
            getRightBtn().setVisibility(View.GONE);
            getRightText2().setVisibility(View.GONE);
            getRightText().setText(R.string.mine_topbar_right);
        }
        if (getTabIndex() == 2) {
            Bundle bundle = new Bundle();
            bundle.putBoolean("isUser", false);
            Intent intent = new Intent(this, HealthActivity.class);
            intent.putExtra("title", getTabText(position));
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            setTitle(getTabText(position));
            contentLayout.removeAllViews();
            AbsBaseView views = viewStack.get(position);
            contentLayout.addView(views);
            views.loadData("");

        }
    }


    public void setCurTab(int index) {
        onClick(tabs[index]);
    }

    public int getTabIndex() {
        return curTab;
    }

    public String getTabText(int index) {
        return tbTvs[index].getText().toString();
    }

    public final static int POP_MENU_CHOOSE_ID = 100;

    @Override
    public void onMenuItemClick(int menuId, int position) {
        if (menuId == POP_MENU_CHOOSE_ID) {
            if (position == 0) {
            } else if (position == 1) {
            } else {
            }
        }
    }
}
